using System;
using System.Collections.Generic;
using System.Text;

namespace manaTools
{
    //struct vor saving project properties
    struct sProjectConfig
    {
        //variables for project Path
        private string pm_ProjectFileName;
        public string m_ProjectPath;
        public string m_ProjectFileNameOnly; //not used ATM, find a use or drop/delete

        public string m_sServerDataDir;
        public string m_sClientDataDir;

        public string m_ProjectFileName
        {
            get { return this.pm_ProjectFileName; }
            set {
                this.pm_ProjectFileName = value;
                string tmpPrPath = "";
                int iSlashCounter = 0;
                int iSlashCounter2 = 0;
                foreach (char tmpCharacter in value)
                {
                    if(tmpCharacter == '\\')
                        iSlashCounter++;
                }
                foreach (char tmpCharacter in value)
                {
                    if (tmpCharacter == '\\')
                        iSlashCounter2++;
                    if (iSlashCounter2 < iSlashCounter)
                    {
                        if(tmpCharacter == '\\')
                            this.m_ProjectPath += '/';
                        else
                            this.m_ProjectPath += tmpCharacter;
                    }
                }
                this.m_ProjectPath += "/";
            }
        }

        //project Variables itself
        public string m_sProjectName;

        public string debug_()
        {
            return "ProjectPath: " + this.m_ProjectPath + "\r\n"
                   +"ProjectFile: " + this.m_ProjectFileName + "\r\n";
        }
        //End of struct
    }

    class Project
    {
        public sProjectConfig m_Config;

        public string debug_()
        {
            return this.m_Config.debug_();
        }

        //Loads the Projectfile at Path
        public bool LoadProject(string Path)
        {
            //checks if the Projectfile exists
            if (System.IO.File.Exists(Path) == true)
            {
                this.m_Config.m_ProjectFileName = Path;

                //opening XML file and reading DATA
                System.Xml.XmlTextReader reader = null;
                reader = new System.Xml.XmlTextReader(Path);

                CSCL.XmlData csclXML = new CSCL.XmlData(Path);
                this.m_Config.m_sProjectName = csclXML.GetElementAsString("project.name");
                this.m_Config.m_sClientDataDir = csclXML.GetElementAsString("project.paths.clientData");
                this.m_Config.m_sServerDataDir = csclXML.GetElementAsString("project.paths.serverData");
                if (pblc.bolDebug == true)
                {
                    pblc.frmDebug.writeText("Following Project-Datas have been Loaded:\r\n");
                    pblc.frmDebug.writeText("ProjectName: "+ this.m_Config.m_sProjectName +"\r\n");
                    pblc.frmDebug.writeText("ClientData-folder: " + this.m_Config.m_sClientDataDir + "\r\n");
                    pblc.frmDebug.writeText("ServerData-folder: " + this.m_Config.m_sServerDataDir + "\r\n");
                    pblc.frmDebug.writeText("Set working Directory to:" + this.m_Config.m_ProjectPath + "\r\n");
                }
                System.IO.Directory.SetCurrentDirectory(pblc.mProject.m_Config.m_ProjectPath);

                if (pblc.bolDebug == true)
                {
                    pblc.frmDebug.writeText("Starting to Load maps.xml From: '" + this.m_Config.m_ProjectPath + this.m_Config.m_sServerDataDir +"maps.xml'\r\n");
                }
                pblc.mMapList.LoadList();
                
                return true;
            }
            else
                return false;//projectFile Doesn't exist
        }
        //End of Class
    }
}
